package com.ebt.m.customer.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.constant.CustomerConstant;
import com.ebt.m.customer.event.EventCustomerListRefresh;
import com.ebt.m.customer.event.EventPolicyCustomerData;
import com.ebt.m.customer.view.VerticalStraightTextView2;
import com.ebt.m.fragment.BaseFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 保单客户列表fragment
 *
 * @author Damon.Han
 */

public class FragmentPolicyCustomer extends BaseFragment {

    @BindView(R.id.list_view)
    ListViewPolicyCustomer mListView;
    @BindView(R.id.iv_empty)
    ImageView mEmptyImageView;
    @BindView(R.id.tv_empty)
    TextView mEmptyTextView;
    @BindView(R.id.btn_empty)
    Button mEmptyButton;
    @BindView(R.id.empty_container)
    LinearLayout mEmptyView;
    @BindView(R.id.fragment_policy_alphabet)
    VerticalStraightTextView2 mAlphabetView;
    @BindView(R.id.fragment_policy_toast)
    TextView mToastView;
    Unbinder unbinder;


    private int mToastViewHeight;
    private int mToastParentViewHeight;

    public static FragmentPolicyCustomer newInstance(int from) {
        FragmentPolicyCustomer frg = new FragmentPolicyCustomer();
        Bundle bundle = new Bundle();
        bundle.putInt("from", from);
        frg.setArguments(bundle);
        return frg;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }


    @Override
    public void onCreateView(Bundle savedInstanceState) {
        setContentView(R.layout.fragment_policy_customers);
        unbinder = ButterKnife.bind(this, contentView);
        mToastViewHeight = mToastView.getLayoutParams().height;
        contentView.postDelayed(new Runnable() {
            @Override
            public void run() {
                initValues();
            }
        }, CustomerConstant.Delay.FRAGMENT_UPDATE_DELAY);
        setAlphabetListener(true);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getView().post(new Runnable() {
            @Override
            public void run() {
                mToastParentViewHeight = ((View) (mToastView.getParent())).getMeasuredHeight();
            }
        });
    }

    private void setAlphabetListener(boolean hasData) {
        if (hasData) {
            mAlphabetView.setOnTouchedCharListener(new VerticalStraightTextView2.OnTouchedCharListener() {

                @Override
                public void onTouchedCharReleased(String releasedString, int releasedIndex) {
                }

                @Override
                public void onTouchedCharChanged(String releasedString, int releasedIndex, String pickedString, int pickedIndex) {
                }

                @Override
                public void onTouchedCharPicked(String pickedString, int pickedIndex) {
                    String sectionLetterActionDown = String.valueOf(CustomerConstant.Sort.CHARS_ALPHA[pickedIndex]);
                    mToastView.setText(sectionLetterActionDown);
                    mToastView.setVisibility(View.VISIBLE);
                    mListView.toMovePositon(sectionLetterActionDown);
//                    mListView.moveToPosition(positionActionDown);
                }

                @Override
                public void onTouchedCharReleasedFinally(String releasedString, int releasedIndex) {
                    mToastView.setVisibility(View.GONE);
                }

                @Override
                public void onTouchedY(float y) {
                    if (y < mToastViewHeight / 2) {
                        mToastView.setTranslationY(-(mToastParentViewHeight - mToastViewHeight) / 2);
                        return;
                    }
                    if (y > mToastParentViewHeight - mToastViewHeight / 2) {
                        mToastView.setTranslationY((mToastParentViewHeight - mToastViewHeight) / 2);
                        return;
                    }
                    mToastView.setTranslationY(y - mToastParentViewHeight / 2);
                }
            });

            if (mAlphabetView.getVisibility() != View.VISIBLE) {
                mAlphabetView.setVisibility(View.VISIBLE);
            }
        } else {
            mAlphabetView.setOnTouchedCharListener(null);
            if (mAlphabetView.getVisibility() != View.GONE) {
                mAlphabetView.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }


    public void initValues() {
        updateData();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventCustomerListRefresh event) {
        if (event != null&&isAdded()) {
            updateData();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventPolicyCustomerData event) {
        if (event != null) {
            setAlphabetListener(event.showAlpha);
        }
    }

    private void updateData() {
        mListView.update(getArguments().get("from"));
    }


}
